<script src="../media/system/js/mootools-core.js" type="text/javascript"></script>
<script src="../media/system/js/core.js" type="text/javascript"></script>
<form action="index.php?top=page&option=com_frcenter" method="post" name="adminForm" id="adminForm">
<!-- LEFT -->
 <?php echo $this->loadTemplate('search','default') ?>
 

 <!-- END LEFT -->
 <!-- CONTENT -->
 <div id="jm-content">
    <div class="cc title">
        <div class="id"><?php echo $this->info_ocba->OCBA_CODE; ?></div>
        <div class="cc-title">
            <p class="top"><?php echo $this->info_ocba->OCBA_KANA; ?></p>
            <p class="bot"><?php echo $this->info_ocba->OCBA_NAME; ?><span class="float-right">【<?php echo $this->info_ocba->OCBA_SHOR; ?>】</span></p>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="menu-cc">
        <ul id="nav">
        
        <?php 
            foreach(bgData::getMenuDetailCenter() as $key=>$item){
                $acctive = 'normal';
                if(JRequest::getVar('task') == $key){
                    $acctive = 'current';
                }
         ?>
            <li class="<?php echo $acctive; ?>"><a href="index.php?top=page&option=com_frcenter&task=<?php echo $key; ?>&id=<?php echo $this->info_ocba->OCBA_CODE; ?>"><?php echo $item; ?></a></li>
          <?php } ?>
            
        </ul>
    </div>
    
    <div id="">     
     <h3 class="h_title">利用人数<span class="right"></span></h3>     
        <div class="info-basic">                                
            <div>
                   <table border=0 width="100%">
                    <tr><td class="title_">活動人数集計</td></tr>
                    <tr>
                       <td align="left" width="150">年度：
                         <select name="year" id="yeamCode">
                           <?php 
                                $year = JRequest::getVar('year');
                                echo HelperBG::getYearOption($year, true);
                            ?>

                         </select>年度
                       </td>
                       <td align="right">
                            <button type="button" onclick="export_csv_statistic();">ダウンロード</button>
                            <button type="button" id="show_chart" onclick="display_chart();">グラフ</button>
                       </td>
                     </tr>
                     
                   </table>
                   <table width="100%" cellpadding="0" cellspacing="0" id="tb_blue" class="tb_small" border=1>
                        <tbody>
                            <?php 
                            $width = (80/13) . "%";
                            $sum_color = "background: #4AC0FF;";
                            foreach($this->result as $usfm_name => $usfm_row){
                                //start header
                                echo    "<tr>";
                                echo    "<th style='width: 4%'>施設</th>".
                                        "<th style='width: 16%'>ジャンル</th>";
                                foreach($usfm_row['head'] as $head){
                                    echo "<th style='width: $width'>$head</th>";
                                }
                                echo    "<th>合計</th>";
                                //end header
                                echo    "</tr>";
                                
                                //start data
                                $first = true;
                                foreach ($this->uscm as $uscm_key => $uscm){
                                    $color = $uscm->USCM_NUMF == 1 ? "background: #ADE2FF;" : "";
                                    //start row
                                    echo "<tr>";
                                    if($first){
                                        echo "<td style='position: relative;' rowspan = '".(count($this->uscm) + 1)."'><div style='position: relative;'><div class='rotate'>$usfm_name</div></div></td>";
                                        $first = false;
                                    } else {
                                        echo "<td style='display:none'>$usfm_name</td>";                                        
                                    }                                    
                                    echo "<td style='$color'>$uscm->USCM_NAME</td>";
                                    foreach($usfm_row['usce_data'][$uscm->USCM_CODE] as $val){
                                        echo "<td class='center' style='$color'>$val</td>";
                                    }
                                    echo "<td class='' style='text-align:center; $color'>" . $usfm_row['sum_row'][$uscm->USCM_CODE] . "</td>";
                                    //end row
                                    echo "</tr>";
                                }
                                //start sum
                                echo "<tr>";
                                echo "<td style='display:none'>$usfm_name</td>";
                                echo "<td class='' style='text-align:left; $sum_color'>人数小計</td>";
                                foreach($usfm_row['sum_col'] as $sum_col){
                                    echo "<td class='' style='text-align:center; $sum_color'>$sum_col</td>";
                                }
                                $total = array_sum($usfm_row['sum_col']) == 0 ? '-' : array_sum($usfm_row['sum_col']);
                                echo "<td class='' style='text-align:center; $sum_color'>$total</td>";
                                echo "</tr>"; 
                            }
                            ?>
                        
                            
                           
                        </tbody>
                    </table>
              
            </div>
            <div id="chart_data" style="display: none;"><?php echo json_encode($this->chart)?></div>
        </div>                               
        
</div>
    
</div>
    <input type="hidden" name="id" value="<?php $id = JRequest::getVar('id'); echo $id?>"/>
    <input type="hidden" name="csv_data" id="csv_data" />
 <!-- END CONTENT -->
</form>

 <script>
jq(document).ready(function(){
    jq('#yeamCode').change(function(event){
        Joomla.submitform('count_data', document.getElementById('adminForm'));
    });

    format_table('#tb_blue');

    
});

function format_table(target){
    jq(target + ' td').each(function(){
        if(!isNaN(parseInt(jq(this).html()))){
            this.style.textAlign = "right";
        }
    });
}

function export_csv_statistic(){
    jq('#csv_data').val(table2csv(jq('#tb_blue')));
    if(jq('#csv_data').val() == '')
        return;
    Joomla.submitform('export', document.getElementById('adminForm'));
}

function table2csv(table){
        var arr = [];
        table.children('tbody').each(function(){
            jq(this).children('tr').each(function(){
                var tmp = [];
                jq(this).children('th, td').each(function(){
                    tmp.push(jq(this).text());
                });
                arr.push(tmp);
            });
            
        });
        
        var csv = [];
        var i = 0;
        jq(arr).each(function(k, v) {
            var row = v.join(",");
            csv.push(row);
        });        
        //csv = encodeURIComponent(csv.join("\r\n"));
        csv = csv.join("\r\n");
        
        return csv;
    }
    
    
function display_chart(){
    
    var tmpl = "<div id='chart_content' style='max-height: 450px; overflow: auto;'> "+
                        "<h2> 使用者人数集計 (グラフ統計)<\/h2> "+
                        "<div id='chart_div' style='height: 500px;'><\/div> "+
                        "<br><br> "+
                        "<div id='chart_table' style='text-align: center;'><\/div> "+
                        "<br> "+
                        "<a style='float: right;' href='#chart_content' >ページの先頭へ<\/a> "+
                    "<\/div> ";
    jq.Zebra_Dialog(tmpl, {
        height: 800,
        width: 1000,
        title:  '利用人数',
        type:  '',
        buttons:  [
            {caption: 'キャンセル', callback: function() {}}
        ]
    });
    drawVisualization();
    
}

function drawVisualization(){
    arr = new Array();
    result = JSON.parse(jq('#chart_data').html());
    var data = google.visualization.arrayToDataTable(rows2cols(result.chart_data));
    var view = new google.visualization.DataView(data);
        
    count = result.count;
    var s = {};
    
    view.hideColumns([count + 1]);

    var options = {
    	vAxis: {title: "利用人数"},
    	seriesType: "bars",
    	series: s,
    	isStacked: true
    };
    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(view, options);
    var tb_data = google.visualization.arrayToDataTable(result.chart_data);
    var table = new google.visualization.Table(document.getElementById('chart_table'));
    table.draw(tb_data, null);
}

function rows2cols(a) {
  var r = [];
  var t;

  for (var i=0, iLen=a.length; i<iLen; i++) {
    t = a[i];

    for (var j=0, jLen=t.length; j<jLen; j++) {
      if (!r[j]) {
        r[j] = [];
      }
      r[j][i] = t[j];
    }
  }
  return r;
}
</script>
                          

